package com.iot.ig.controller;

import com.iot.ig.websocket.handler.IotWebSocketHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class WebSocketController {

    @Resource
    private IotWebSocketHandler iotWebSocketHandler;

    /**
     * 广播消息
     * @param message
     * @return
     */
    @PostMapping("/sendMessage")
    public String sendMessage(@RequestParam String message) {
        iotWebSocketHandler.broadcastMessage(message);
        return "Message sent: " + message;
    }

    /**
     * 给某个用户发送消息
     * @param username
     * @param message
     * @return
     */
    @PostMapping("/sendMessageToUser")
    public String sendMessageToUser(@RequestParam String username, @RequestParam String message) {
        iotWebSocketHandler.sendMessageToUser(username, message);
        return "Message sent to user: " + username + ", Message: " + message;
    }
}
